package ua.com.infinite.mobile.slidingmenu.fragments;

import ua.com.infinite.mobile.R;
import ua.com.infinite.mobile.adapters.AdapterTransaktions;
import ua.com.infinite.mobile.server.OnUpdateUiListener;
import ua.com.infinite.mobile.ui.pulltorefresh.PullToRefreshBase;
import ua.com.infinite.mobile.ui.pulltorefresh.PullToRefreshListView;
import ua.com.infinite.mobile.ui.pulltorefresh.PullToRefreshBase.Mode;
import ua.com.infinite.mobile.ui.pulltorefresh.PullToRefreshBase.OnLastItemVisibleListener;
import ua.com.infinite.mobile.ui.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;

public class FragmentTransaktions extends ListFragment implements OnUpdateUiListener {

	public static final String IS_FIRST_REFRESHING = "is_first_refreshing";
	public static final String IS_NO_OLD_NEWS = "no_old";
	public static final String REFRESHING_MODE = "refreshing_mode";

	private String tag;
	private AdapterTransaktions adapter;
	private PullToRefreshListView refreshableList;
	private ProgressBar progressBar;
	private ListView list;
	private Mode refreshingMode = Mode.DISABLED;

	private boolean isNoOldNews = false;
	private boolean isFirstRefresh = true;

	public static FragmentTransaktions newInstance(String tag) {
		Bundle args = new Bundle();
		args.putString("tag", tag);
		FragmentTransaktions fr = new FragmentTransaktions(tag);
		fr.setArguments(args);

		return fr;
	}

	public FragmentTransaktions(String tag) {
		this.tag = tag;
		// setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tarnsaktions_layout, null);
		progressBar = (ProgressBar) view.findViewById(R.id.empty_list_progress);
		progressBar.setVisibility(ProgressBar.VISIBLE);
		refreshableList = (PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
		refreshableList.setVisibility(PullToRefreshListView.GONE);
		return view;

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (savedInstanceState != null) {
			tag = savedInstanceState.getString("tag");
			isFirstRefresh = savedInstanceState.getBoolean(tag + IS_FIRST_REFRESHING);
			isNoOldNews = savedInstanceState.getBoolean(tag + IS_NO_OLD_NEWS);
			refreshingMode = Mode.mapIntToValue(savedInstanceState.getInt(tag + REFRESHING_MODE));
		} else {
			tag = this.getArguments().getString("tag");
		}

		adapter = new AdapterTransaktions(getActivity(), null, this, tag);
		updateUI(true);

		refreshableList.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
				refreshingMode = Mode.PULL_DOWN_TO_REFRESH;
				/*if (adapter.getCount() != 0)
					adapter.updateByTyme(Constants.NEWS_NEW);
				refreshableList.setLastUpdatedLabel(DateUtils.formatDateTime(getActivity().getApplicationContext(), System.currentTimeMillis(),
						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL));*/
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
				refreshingMode = Mode.PULL_UP_TO_REFRESH;
				/*if (adapter.getCount() != 0)
					adapter.updateByTyme(Constants.NEWS_OLD);*/
			}
		});

		refreshableList.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

			@Override
			public void onLastItemVisible() {
				/*if (!isNoOldNews && adapter.getCount() != 0) {
					refreshingMode = Mode.PULL_UP_TO_REFRESH;
					refreshableList.setMode(Mode.PULL_UP_TO_REFRESH);
					refreshableList.setRefreshing(false);
					adapter.updateByTyme(Constants.NEWS_OLD);
				}*/
			}
		});

		refreshableList.setDisableScrollingWhileRefreshing(false);
		//refreshableList.setMode(Mode.BOTH);
		refreshableList.setMode(Mode.DISABLED);

		list = refreshableList.getRefreshableView();
		list.setAdapter(adapter);
		list.setOnItemClickListener(listListener);

	}

	OnItemClickListener listListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
			/*Intent intent = new Intent(getActivity(), ActivityNewsDetailed.class);
			NewsItem item = db.getNewsItem(view.getId());
			intent.putExtra("news_item", item);
			startActivity(intent);*/
		}
	};

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("tag", tag);
		outState.putBoolean(tag + IS_FIRST_REFRESHING, isFirstRefresh);
		outState.putBoolean(tag + IS_NO_OLD_NEWS, isNoOldNews);
		outState.putInt(tag + REFRESHING_MODE, refreshingMode.ordinal());
	}

	@Override
	public void updateUI(boolean isSucceful) {
		if (adapter.getCount() != 0) {
			progressBar.setVisibility(ProgressBar.GONE);
			refreshableList.setVisibility(PullToRefreshListView.VISIBLE);
		} else {
			progressBar.setVisibility(ProgressBar.VISIBLE);
			refreshableList.setVisibility(PullToRefreshListView.GONE);
		}

		if (isFirstRefresh)
			isFirstRefresh = false;

		refreshableList.onRefreshComplete();
		//refreshableList.setMode(Mode.BOTH);
	}
}
